package com.ruoyi.commerce.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.commerce.domain.vo.CommerceOrdersVo;
import com.ruoyi.commerce.mapper.CommerceOrdersMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.commerce.domain.bo.CommercePurchaseBo;
import com.ruoyi.commerce.domain.vo.CommercePurchaseVo;
import com.ruoyi.commerce.domain.CommercePurchase;
import com.ruoyi.commerce.mapper.CommercePurchaseMapper;
import com.ruoyi.commerce.service.ICommercePurchaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采购单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-15
 */
@RequiredArgsConstructor
@Service
public class CommercePurchaseServiceImpl implements ICommercePurchaseService {

    private final CommercePurchaseMapper baseMapper;

    /**
     * 查询采购单
     */
    @Override
    public CommercePurchaseVo queryById(Long purchaseId){
        return baseMapper.selectVoById(purchaseId);
    }

    /**
     * 查询采购单
     */
    @Override
    public List<CommercePurchaseVo> queryByIds(Collection<Long> purchaseIds){
        return baseMapper.selectVoBatchIds(purchaseIds);
    }

    /**
     * 查询采购单列表
     */
    @Override
    public TableDataInfo<CommercePurchaseVo> queryPageList(CommercePurchaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommercePurchase> lqw = buildQueryWrapper(bo);
        Page<CommercePurchaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采购单列表
     */
    @Override
    public List<CommercePurchaseVo> queryList(CommercePurchaseBo bo) {
        LambdaQueryWrapper<CommercePurchase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CommercePurchase> buildQueryWrapper(CommercePurchaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CommercePurchase> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderDate() != null, CommercePurchase::getOrderDate, bo.getOrderDate());
        lqw.eq(bo.getArrivalDate() != null, CommercePurchase::getArrivalDate, bo.getArrivalDate());
        lqw.eq(bo.getTotalAmount() != null, CommercePurchase::getTotalAmount, bo.getTotalAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CommercePurchase::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增采购单
     */
    @Override
    public CommercePurchaseBo insertByBo(CommercePurchaseBo bo) {
        CommercePurchase add = BeanUtil.toBean(bo, CommercePurchase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPurchaseId(add.getPurchaseId());
            bo.setStatus(add.getStatus());
        }
        return bo;
    }

    /**
     * 修改采购单
     */
    @Override
    public Boolean updateByBo(CommercePurchaseBo bo) {
        CommercePurchase update = BeanUtil.toBean(bo, CommercePurchase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CommercePurchase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采购单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
